package com.simple.datax.service.impl;

import java.util.List;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.SimpleFailMessage;
import com.simple.datax.SimpleMessage;
import com.simple.datax.SimpleObject;
import com.simple.datax.api.MessageProcessor;
import com.simple.datax.service.DBSystemLogService;
import com.simple.datax.service.ResponseMessageService;

/** 
 * @Title: SendStatusHandler.java 
 * @Package com.simple.datax.service.impl 
 * @Description: 提供针对各种发送状态的处理
 * @author Jin Yi 
 * @date 2012-12-30
 * @version V1.0
 */
public class SendStatusHandler {
	private static final Logger logger = LoggerFactory
			.getLogger(SendStatusHandler.class);
	private static SendStatusHandler instance;
	private MessageProcessor failService;
	private DBSystemLogService dbLogService;
	private ResponseMessageService responseService;
	private MessageProcessor successService;
	private List<MessageProcessor> backupServices;
	
	public SendStatusHandler() {
		instance = this;
	}
	
	public synchronized static SendStatusHandler getInstance() {
		if (instance==null) {
			instance = new SendStatusHandler();
		}
		return instance;
	}
	public MessageProcessor getFailService() {
		return failService;
	}
	public void setFailService(MessageProcessor failService) {
		this.failService = failService;
	}
	public DBSystemLogService getDbLogService() {
		return dbLogService;
	}
	public void setDbLogService(DBSystemLogService dbLogService) {
		this.dbLogService = dbLogService;
	}
	public ResponseMessageService getResponseService() {
		return responseService;
	}
	public void setResponseService(ResponseMessageService responseService) {
		this.responseService = responseService;
	}
	public MessageProcessor getSuccessService() {
		return successService;
	}
	public void setSuccessService(MessageProcessor successService) {
		this.successService = successService;
	}
	
	/**
	 * 在数据库中记载处理失败信息
	 * 
	 * @param src
	 */
	public void callFailService(SimpleObject src) {
		if (!(src instanceof SimpleFailMessage)) {
			logger.error("The Paramter MUST be SimpleFailMessage!");
			return;
		}

		if (failService != null) {
			try {
				failService.process(src);
				logger.debug("Fail service: " + ((SimpleMessage)src).toLog());
			} catch (Exception e) {
				logger.error("Error Occur When Call "
						+ failService.getClass().getName(), e);
			}
		} else {
			logger.info("There is no Fail proc Service!!!!");
		}
	}
	
	/**
	 * 数据发送成功处理
	 * @param src
	 */
	public void callSuccessService(SimpleObject src) {
		if (!(src instanceof SimpleMessage)) {
			logger.error("The Paramter MUST be SimpleMessage!");
			return;
		}

		if (successService != null) {
			// 调用成功服务处理
			try {
				successService.process(src);
				logger.debug("Success service: " + ((SimpleMessage)src).toLog());
			} catch (Exception e) {
				logger.error("Error Occur When Call "
						+ failService.getClass().getName(), e);
			}
			
			if (backupServices == null || backupServices.size() < 1) {
				logger.info("There are no backup Services!!!!");
				return;
			}
			
			// 调用备份处理
			for (MessageProcessor mp : backupServices) {
				if (mp != null) {
					try {
						mp.process(src);
						logger.debug("Backup service: " + ((SimpleMessage)src).toLog());
					} catch (Exception e) {
						logger.error("Error Occur When Call " + mp.getClass().getName(), e);
					}
				}
			}
		} else {
			logger.info("There is no Success proc Service!!!!");
		}
	}
	
	/**
	 * 用来发送回执信息
	 * 
	 * @param src
	 * @param responseCode
	 * @param responseMsg
	 */
	public void callResponseService(SimpleMessage src, int responseCode,
			String responseMsg, boolean isMust) {
		if (responseService == null) {
			logger.info("There is no responseMessage Service!");
		} else {
			try {
				responseService
						.response(src, responseCode, responseMsg, isMust);
			} catch (Exception e) {
				logger.error("Send Response message Error:", e);
			}
		}
	}
	
	public List<MessageProcessor> getBackupServices() {
		return backupServices;
	}
	
	public void setBackupServices(List<MessageProcessor> backupServices) {
		this.backupServices = backupServices;
	}
}
